package com.yun.lib.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yun.lib.R;


/**
 * Created by Administrator on 2017/4/6 title_text 标题.
 * title_text 标题
 * title_text_color 标题颜色
 * backgroundColor 标题栏背景
 * left_drawable 左边图片 默认 <
 * left_text 左边文字
 * left_text_color
 * left_visible
 * 右边参数同理
 * right_background 右边背景
 * right_icon 右边第二个图片
 * setOnLeftClickListener
 * setOnRightClickListener
 * setOnRightIcon2ClickListener
 */

public class CustomTitleBar extends LinearLayout {

    private TextView back;
    private TextView act;
    private TextView title;
    private View titleLay;
    private boolean leftNormal = true;
    private ImageView rightIcon2;

    public CustomTitleBar(Context context) {
        super(context);
    }

    public CustomTitleBar(final Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_title, this);
        titleLay = findViewById(R.id.title_lay);
        back = (TextView) findViewById(R.id.title_left);
        act = (TextView) findViewById(R.id.title_right);
        title = (TextView) findViewById(R.id.title_text);
        rightIcon2 = (ImageView) findViewById(R.id.right_icon2);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftNormal)
                    if (leftNormal)
                        ((Activity) context).finish();
            }
        });


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
        if (typedArray != null) {
            int backgroundColor = typedArray.getColor(R.styleable.CustomTitleBar_backgroundColor, -1);
            if (backgroundColor != -1) {
                titleLay.setBackgroundColor(backgroundColor);
            }

            String titleText = typedArray.getString(R.styleable.CustomTitleBar_title_text);
            if (!TextUtils.isEmpty(titleText)) {
                title.setText(titleText);
            }
            int customTitleBar_title_text_color = R.styleable.CustomTitleBar_title_text_color;
            int titleColor = typedArray.getColor(R.styleable.CustomTitleBar_title_text_color, 1);
            title.setTextColor(titleColor);

            //左边按钮
            if (typedArray.getBoolean(R.styleable.CustomTitleBar_left_visible, true)) {
                back.setVisibility(VISIBLE);
                String textLeft = typedArray.getString(R.styleable.CustomTitleBar_left_text);
                if (!TextUtils.isEmpty(textLeft)) {
                    back.setText(textLeft);
                    back.setCompoundDrawables(null, null, null, null);
                    int leftColor = typedArray.getColor(R.styleable.CustomTitleBar_left_text_color, Color.BLACK);
                    back.setTextColor(leftColor);
                    leftNormal = false;
                } else {
                    int leftDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_left_drawable, 0);
                    if (leftDrawable != -1 && leftDrawable != 0) {
                        Drawable drawable = getResources().getDrawable(leftDrawable);
                        drawable.setBounds(0, 0, 60, 60);
                        back.setCompoundDrawables(drawable, null, null, null);
                    }
                }
            } else {
                back.setVisibility(INVISIBLE);
            }

            //右边按钮
            String textRight = typedArray.getString(R.styleable.CustomTitleBar_right_text);
            if (!TextUtils.isEmpty(textRight)) {
                act.setText(textRight);
                int rightColor = typedArray.getColor(R.styleable.CustomTitleBar_right_text_color, Color.BLACK);
                act.setTextColor(rightColor);
                int resourceId = typedArray.getResourceId(R.styleable.CustomTitleBar_right_background, -1);
                if (resourceId != -1) {
                    act.setBackgroundResource(resourceId);
                }
            } else {
                int rightDrawable = typedArray.getResourceId(R.styleable.CustomTitleBar_right_drawable, 0);
                if (rightDrawable != -1 && rightDrawable != 0) {
                    Drawable drawable = getResources().getDrawable(rightDrawable);
                    drawable.setBounds(0, 0, 60, 60);
                    act.setCompoundDrawables(null, null, drawable, null);
                }
            }

            //右边有两个
            int rightIconId = typedArray.getResourceId(R.styleable.CustomTitleBar_right_icon, -1);
            if (rightIconId != -1) {
                rightIcon2.setVisibility(VISIBLE);
                rightIcon2.setImageResource(rightIconId);
            }

            typedArray.recycle();
        }

    }

    public void setOnLeftClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            leftNormal = false;
            back.setOnClickListener(onClickListener);
        }
    }

    public void setOnRightClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            act.setOnClickListener(onClickListener);
        }
    }

    public void setOnRightIcon2ClickListener(OnClickListener onClickListener) {
        if (onClickListener != null) {
            rightIcon2.setOnClickListener(onClickListener);
        }
    }

    public TextView getLeftText() {
        return back;
    }

    public TextView getRightText() {
        return act;
    }

    public TextView getTitle() {
        return title;
    }

    public void setTitle(String titleText) {
        title.setText(titleText);
    }

    public void setleftText(String titleText) {
        back.setText(titleText);
    }
}
